<script type="text/javascript">
$('form').submit(function() {
  if(!$('form').validationEngine({returnIsValid:true})===true){
        $("#body").load($(this).attr("action"),$(this).serialize(),function(){$('#example').dataTable()});
    }
  return false;
});
</script>
<form action="index.php?page=CMS&action=saveSection" method="post">
    <table>
        <tr>
            <td>
                Section name
            </td>
            <td>
                <input type="text" name="name"  id="name" class="validate[required,length[0,50]]" <?php if(isset($section)) echo "value ='".$section->name."' ";?>/>
                <?php if(isset($section)) echo "<input type='hidden' name='id' id='id' value='{$section->id}'";?>
            </td>
        </tr>
        <tr>
            <td>
                Section Type
            </td>
            <td>
                <select name="type">
                    <?php foreach($sectionTypes as $type){?>
                        <option value="<?php echo $type->id?>" <?php if(isset($section)) if($section->sectionTypeId == $type->id) echo "selected";?>><?php echo $type->name?></option>
                    <?php }?>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                Section Status
            </td>
            <td>
                <input type="radio" name="status" value="0" <?php if(isset($section)) {if($section->status_ == 0) echo "checked";} else echo "checked"; ?>/> closed &nbsp;&nbsp
                <input type="radio" name="status" value="1" <?php if(isset($section)) {if($section->status_ == 1) echo "checked";} ?> /> open
            </td>
        </tr>
        <tr>
            <td>
                Section Order
            </td>
            <td>
                <input type="text" name="order" id="order" class="validate[required,custom[integer]] text-input" <?php if(isset($section)) echo "value='".$section->order_."'"; else echo "value='0'";?>/>
            </td>
        </tr>

        <?php
            if(isset($section)){
                if(count($section->subsections)>0)
                {
                    echo "<tr>";

                    echo "<td>";
                        echo "Main subsection";
                    echo "</td>";

                    echo "<td>";
                        echo "<select name='mainSubsection'>";
                        echo "<option value='0'>None</option>";
                        foreach($section->subsections as $subsection){
                            $selected = "";
                            if($subsection->id == $section->mainSubSectionId)
                            $selected="selected";
                            echo "<option value='".$subsection->id."' {$selected}>".$subsection->name."</option>";
                        }
                        echo "<select/>";
                    echo "</td>";

                    echo "</tr>";
                }
            }
        ?>
        <tr>
            <td>
                <input type="submit" value="save"/>
            </td>
        </tr>
    </table>
</form>
